package com.gomore.opple.module.flash;

import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.bumptech.glide.request.target.Target;
import com.gomore.opple.R;
import com.gomore.opple.module.BaseActivity;
import com.gomore.opple.module.IntentStart;
import com.gomore.opple.utils.DensityUtil;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;

/**
 * Created by asus on 2017/5/9.
 */
public class FlashActivity extends BaseActivity {
    @Bind(R.id.flash)
    ImageView flash;
    private TimerTask timerTask;
    private int recordTime;

    @Override
    protected void initalizeViews() {
        startTextViewAnim();
    }

    @Override
    protected void initializeDependencyInjector() {

    }

    @Override
    protected int getLayoutView() {
        setTheme(R.style.AppTheme_Launcher);
        return R.layout.activity_flash;
    }

    private void startTextViewAnim() {
        RequestListener listener = new RequestListener<Integer, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, Integer model, Target<GlideDrawable> target, boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable resource, Integer model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                starTimer();
                return false;
            }
        };
        int width = DensityUtil.screenWith(FlashActivity.this);
        int height = DensityUtil.screenHeight(FlashActivity.this);
        Glide.with(this)
                .load(R.drawable.flash)
                .listener(listener)
                .override(width, height)
                .into(new GlideDrawableImageViewTarget(flash, 1));
    }

    /**
     * 开启计时
     */
    private void starTimer() {
        timerTask = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        recordTime++;
                        if (recordTime == 4) {
                            if (timerTask != null) {
                                timerTask.cancel();
                                IntentStart.getInstance().startLoginActivity(FlashActivity.this);
                            }
                        }
                    }
                });
            }
        };
        Timer recordTimer = new Timer(true);
        recordTimer.schedule(timerTask, 0, 1000);
    }
}
